package com.yict.common.util;

import java.util.Locale;
import java.util.ResourceBundle;

/**
 * <h3>Class name</h3> <h4>Description</h4>
 * 
 * <h4>Special Notes</h4>
 * 
 * 
 * @ver 0.1
 * @author Jay.Wu 2008-9-10
 * 
 */
public class ResourceBundleUtil {
	public static String ApplicationResourcesPre = "ApplicationResources";

	protected static ResourceBundle bundle = ResourceBundle.getBundle(ApplicationResourcesPre);

	protected static ResourceBundle rBundle = ResourceBundle.getBundle(ApplicationResourcesPre);

	protected static Locale locale = Locale.getDefault();

	public static String getString(String key) {
		try {
			return bundle.getString(key);
		} catch (Exception e) {
		}

		return key;
	}

	public static Locale getLocale() {
		return bundle.getLocale();
	}

	public static String getString(String key, String language) {
		if (!locale.getLanguage().equals(language)) {
			locale = new Locale(language);
			rBundle = ResourceBundle.getBundle(ApplicationResourcesPre, locale);
		}
		try {
			return rBundle.getString(key);
		} catch (Exception e) {
		}

		return key;
	}

	public void setString(String key, String language, String value) {
		EnvironmentUtil environmentUtil = new EnvironmentUtil(ApplicationResourcesPre + "_" + language + ".properties");
		environmentUtil.setValue(key, value);
		environmentUtil.saveFile();
		this.getClass().getClassLoader().clearAssertionStatus();
	}
}
